package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @PostMapping
    public R saveWithDish(@RequestBody SetmealDto dto){
        setmealService.saveWithDish(dto);
        return R.success("添加成功");
    }
    //分页查询
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        Page<SetmealDto> pageInfo= setmealService.pageByName(page,pageSize,name);
        return R.success(pageInfo);
    }

    //删除--根据id的集合来删除
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids){
        log.info("删除套餐ids:{}",ids);
        setmealService.deleteByIds(ids);
        return R.success("杀出成功");
    }

    //查询套餐列表
    @GetMapping("/list")
    public R list( Setmeal setmeal){
        Long categoryId = setmeal.getCategoryId();//前端传递过来的
        Integer setmealStatus = setmeal.getStatus();
        //构建条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //根据分类id精确查询
        lqw.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        //根据状态精确查询
        lqw.eq(setmealStatus!=null,Setmeal::getStatus,setmealStatus);
        //排序
        lqw.orderByDesc(Setmeal::getUpdateTime);
        //执行查询
        List<Setmeal> list = setmealService.list(lqw);

        return R.success(list);

    }

}    